package com.luojialong.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luojialong.enums.AffairStatus;
import com.luojialong.exception.AffairException;
import com.luojialong.model.domain.User;
import com.luojialong.model.request.user.UserLoginRequest;
import com.luojialong.model.request.user.UserQueryRequest;
import com.luojialong.model.request.user.UserRegisterRequest;
import com.luojialong.model.vo.user.LoginUserVo;
import com.luojialong.model.vo.user.UserVo;
import com.luojialong.resultVo.ResultResponse;
import com.luojialong.service.UserService;
import com.luojialong.utils.ResultUtils;
import com.luojialong.verification.MyCreateLog;
import com.luojialong.verification.UserPermissionCheck;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.luojialong.constant.UserRank.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 用户注册
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/register")
    @MyCreateLog(doMethod = "用户注册")
    public ResultResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new AffairException(AffairStatus.PARAMS_ERROR, "用户请求参数与后端不对应");
        }
        long res = userService.userRegister(userRegisterRequest);
        return ResultUtils.success(res);
    }


    /**
     * 用户登录
     * @param userLoginRequest 用户登录请求,账号,密码
     * @param request 请求
     * @return 返回视图层的用户信息
     */
    @PostMapping("/login")
    @MyCreateLog(doMethod = "用户登录")
    public ResultResponse<LoginUserVo> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new AffairException(AffairStatus.PARAMS_ERROR, "用户请求参数与后端不对应");
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        User user = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(LoginUserVo.transferVo(user));
    }

    /**
     * 用户的账户注销
     * @param request 请求
     * @return 1代表注销成功,否则失败
     */
    @PostMapping("/logout")
    @MyCreateLog(doMethod = "用户的账户注销")
    public ResultResponse<Integer> userLogout(HttpServletRequest request) {
        if (request == null) {
            throw new AffairException(AffairStatus.PARAMS_ERROR, "用户请求参数与后端不对应");
        }
        Integer i = userService.userLogout(request);
        return ResultUtils.success(i);
    }


    /**
     * 权限更新
     * @param request 请求
     * @return 返回更新权限后的用户信息
     */
    @GetMapping("/current")
    @MyCreateLog(doMethod = "更新权限")
    public ResultResponse<UserVo> getCurrentUser(HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(USER_LOGIN_KEY);
        User currentUser = (User) userObj;
        if (currentUser == null) {
            throw new AffairException(AffairStatus.NOT_LOGIN, "用户未登录");
        }
        User updateUser = userService.getById(currentUser.getId());
        request.getSession().setAttribute(USER_LOGIN_KEY,updateUser);
        return ResultUtils.success(UserVo.transferVo(updateUser));
    }


    @GetMapping("/search")
    @MyCreateLog(doMethod = "高级用户查询普通用户")
    @UserPermissionCheck(LeastPrivilege = ADMINISTRATOR_OF_THE_EDUCATION_BUREAU)
    public ResultResponse<Page<UserVo>> searchUsers(UserQueryRequest userQueryRequest, HttpServletRequest request) {
        long current = userQueryRequest.getCurrent();
        long pageSize = userQueryRequest.getPageSize();
        Page<User> userPage = userService.page(new Page<>(current,pageSize),userService.getPageQueryWrapper(userQueryRequest));
        return ResultUtils.success(userService.getPageVo(userPage));
    }

    @PostMapping("/delete")
    @MyCreateLog(doMethod = "执行删除方法")
    @UserPermissionCheck(LeastPrivilege = ADMINISTRATOR_OF_THE_EDUCATION_BUREAU)
    public ResultResponse<Boolean> deleteUser(@RequestBody Long id, HttpServletRequest request) {
        if (id == null || id < 0) {
            throw new AffairException(AffairStatus.PARAMS_ERROR, "请求参数与后端不对应");
        }
        boolean b = userService.removeById(id);
        return ResultUtils.success(b);
    }


}
